package org.kernely.ui.profile;

import java.util.ArrayList;

import org.kernely.ui.MainApplication;
import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventReceiver;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.data.validator.EmailValidator;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Field;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class KernelyProfileView extends GridLayout implements IKernelyView, KernelyEventSender {

	/**
	 * The Serial Version
	 */
	private static final long serialVersionUID = -5465377098197707204L;


	private VerticalLayout pictureLayout;
	private VerticalLayout infoLayout;
	private HorizontalLayout buttons;

	private Label title;
	private Label pictureTitle;
	private Label infoTitle;

	private Embedded pictureEmbedded;
	private Upload uploadPicture;
	private Button resetPictureButton;
	private Button cancelButton;
	private Button saveButton;
	private TextField nameField;
	private TextField firstnameField;
	private TextField emailField;
	private ComboBox civility;

	/**
	 * KernelyProfileView's constructor
	 */
	public KernelyProfileView(){

		pictureLayout = new VerticalLayout();
		infoLayout = new VerticalLayout();
		buttons = new HorizontalLayout();
		title = new Label("Profile");
		title.setStyleName(Reindeer.LABEL_H1);
		title.setSizeUndefined(); // Usefull to center the label.
		infoTitle = new Label("Your personnal informations");
		infoTitle.setStyleName(Reindeer.LABEL_H2);
		infoTitle.setSizeUndefined();
		pictureTitle = new Label("Your picture");
		pictureTitle.setStyleName(Reindeer.LABEL_H2);
		pictureTitle.setSizeUndefined();
		pictureEmbedded = new Embedded();
		pictureEmbedded.setHeight(300,Sizeable.UNITS_PIXELS);
		pictureEmbedded.setWidth(300,Sizeable.UNITS_PIXELS);
		pictureEmbedded.setImmediate(true);

		uploadPicture = new Upload("Upload a new picture",null);
		resetPictureButton = new Button("Reset picture");
		saveButton = new Button("Save changes");
		saveButton.setClickShortcut(KeyCode.ENTER);
		cancelButton = new Button("Cancel");

		civility = new ComboBox("Civility");
		civility.setNullSelectionAllowed(false);
		civility.addItem(0);
		civility.setItemCaption(0, "Mr.");
		civility.addItem(1);
		civility.setItemCaption(1, "Mrs.");
		civility.addItem(2);
		civility.setItemCaption(2, "Ms.");
		nameField = new TextField("Name");
		firstnameField = new TextField("Firstname");
		emailField = new TextField("E-mail");

		emailField.setRequired(true);
		nameField.setRequired(true);
		firstnameField.setRequired(true);

		// Error messages displayed in the presenter when field are empty
		emailField.setRequiredError("E-mail can't be empty.");
		nameField.setRequiredError("Name can't be empty.");
		firstnameField.setRequiredError("Firstname can't be empty.");

		pictureLayout.addComponent(pictureTitle);
		pictureLayout.addComponent(pictureEmbedded);
		pictureLayout.addComponent(uploadPicture);
		pictureLayout.addComponent(resetPictureButton);
		pictureLayout.setComponentAlignment(pictureTitle, Alignment.MIDDLE_CENTER);
		pictureLayout.setComponentAlignment(pictureEmbedded, Alignment.MIDDLE_CENTER);
		pictureLayout.setComponentAlignment(uploadPicture, Alignment.MIDDLE_CENTER);
		pictureLayout.setComponentAlignment(resetPictureButton, Alignment.MIDDLE_CENTER);


		infoLayout.addComponent(infoTitle);
		infoLayout.setComponentAlignment(infoTitle, Alignment.MIDDLE_CENTER);

		infoLayout.addComponent(civility);
		infoLayout.addComponent(nameField);
		infoLayout.addComponent(firstnameField);
		infoLayout.addComponent(emailField);

		infoLayout.setComponentAlignment(civility, Alignment.MIDDLE_CENTER);
		infoLayout.setComponentAlignment(nameField, Alignment.MIDDLE_CENTER);
		infoLayout.setComponentAlignment(firstnameField, Alignment.MIDDLE_CENTER);
		infoLayout.setComponentAlignment(emailField, Alignment.MIDDLE_CENTER);

		buttons.addComponent(cancelButton);
		buttons.addComponent(saveButton);
		buttons.setComponentAlignment(cancelButton, Alignment.MIDDLE_CENTER);
		buttons.setComponentAlignment(saveButton, Alignment.MIDDLE_CENTER);


		this.setColumns(2);
		this.setRows(4);
		this.addComponent(title,0,0,1,0);
		this.addComponent(pictureTitle,0,1);
		this.addComponent(infoTitle,1,1);
		this.addComponent(pictureLayout,0,2);
		this.addComponent(infoLayout,1,2);
		this.addComponent(buttons,0,3,1,3);
		this.setComponentAlignment(title,Alignment.MIDDLE_CENTER);
		this.setComponentAlignment(infoTitle,Alignment.MIDDLE_CENTER);
		this.setComponentAlignment(pictureTitle,Alignment.MIDDLE_CENTER);
		this.setComponentAlignment(pictureLayout, Alignment.MIDDLE_CENTER);
		this.setComponentAlignment(infoLayout, Alignment.MIDDLE_CENTER);
		this.setComponentAlignment(buttons, Alignment.MIDDLE_CENTER);
		this.setSizeFull();
		this.emailField.addValidator(new EmailValidator("This e-mail is not valid!"));		
	}

	/**
	 * This method is called when this component is added to an other.
	 */
	@Override
	public final void attach(){
		super.attach();
		//When the component is attached, we fire listeners
		MainApplication.getEventBus().broadcastEvent(new KernelyProfileAttachedEvent(this));
	}

	/**
	 * @see org.kernely.core.service.IKernelyView#getComponent()
	 */
	@Override
	public final Component getComponent() {
		return this;
	}

	/**
	 * Gets the embedded picture
	 * @return the pictureEmbedded
	 */
	public final Embedded getPictureEmbedded() {
		return pictureEmbedded;
	}

	/**
	 * Gets the uploadPicture
	 * @return the uploadPicture
	 */
	public final Upload getUploadPicture() {
		return uploadPicture;
	}

	/**
	 * Gets the resetPictureButton
	 * @return the resetPictureButton
	 */
	public final Button getResetPictureButton() {
		return resetPictureButton;
	}

	/**
	 * Gets the cancelButton
	 * @return the cancelButton
	 */
	public final Button getCancelButton() {
		return cancelButton;
	}

	/**
	 * Gets the saveButton
	 * @return the saveButton
	 */
	public final Button getSaveButton() {
		return saveButton;
	}

	/**
	 * Gets the nameField
	 * @return the nameField
	 */
	public final Field getNameField() {
		return nameField;
	}

	/**
	 * Gets the firstnameField
	 * @return the firstnameField
	 */
	public final Field getFirstnameField() {
		return firstnameField;
	}

	/**
	 * Gets the emailField
	 * @return the emailField
	 */
	public final Field getEmailField() {
		return emailField;
	}

	/**
	 * Gets the civility
	 * @return the civility
	 */
	public final ComboBox getCivility() {
		return civility;
	}

}
